package org.nudt.system.service

import jakarta.persistence.criteria.Predicate
import org.nudt.common.model.PageData
import org.nudt.common.model.User
import org.nudt.common.repository.UserRepository
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.domain.Specification
import org.springframework.stereotype.Service

@Service
class SysUserService(private val userRepository: UserRepository) {
    
    
    fun getUserList(nickname: String?, gender: Int?, appVersion: String?, osVersion: String?, pageable: Pageable): PageData<User> {
        val spec = Specification<User> { root, query, cBuilder ->
            val predicates: MutableList<Predicate> = ArrayList()
            nickname?.let {
                val condition = cBuilder.like(root.get("nickname"), "%${it}%")
                predicates.add(condition)
            }
            gender?.let {
                val condition = cBuilder.equal(root.get<Int>("gender"), it)
                predicates.add(condition)
            }
            appVersion?.let {
                val condition = cBuilder.like(root.get("nickname"), "%${it}%")
                predicates.add(condition)
            }
            osVersion?.let {
                val condition = cBuilder.like(root.get("osVersion"), "%${it}%")
                predicates.add(condition)
            }
            // 返回查询条件
            return@Specification cBuilder.and(*predicates.toTypedArray())
        }
        val result = userRepository.findAll(spec, pageable)
        
        return PageData(result.number + 1, result.size, result.totalElements, result.content)
    }
}